package Z.Service;

import Z.Dao.userSDao;
import Z.Entity.userS;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取传递参数
        String name = request.getParameter("name");
        String pass = request.getParameter("pass");
        if (null != name && !name.equals("") && null != pass && !pass.equals("")) {
            userS a = new userS();
            a.setUsername(name);
            a.setPassword(pass);
            boolean b = userSDao.isLogin(a);
            if (b) {
                //创建一个会话，防止非法登录，绑定也给值
                HttpSession session = request.getSession();//
                session.setAttribute("loginName", name);//将登录用户和loginName绑定
                response.sendRedirect("/Z/HosIndex.jsp");
            } else {
                response.sendRedirect("/Z/login.jsp");
            }
        }else {
            response.sendRedirect("/Z/login.jsp");
        }
    }
}
